from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import ShouJi, CaXun
# Create your views here.


def index(request):

    if request.method == 'POST':
        lianxi = request.POST['lianxi']
        if not lianxi:
            number = ShouJi.objects.filter(tianjia__exact="No").count()
            context = {
                "show": "警告！！！请不要提交空白名，不然我会奖励你一朵小红花的哦",
                "numder": number,
            }
            return render(request, 'index.html', context=context)
        elif not ShouJi.objects.filter(lianxi__exact=lianxi):
            shouji = ShouJi(lianxi=lianxi)
            shouji.save()
            number = ShouJi.objects.filter(tianjia__exact="No").count()
            context = {
                "show": "您的联系方式我已收到，不要重复留下您宝贵的联系方式，否则我会不想理你的-宝~",
                "numder": number,
            }
            return render(request, 'index.html', context=context)
        else:
            number = ShouJi.objects.filter(tianjia__exact="No").count()
            context = {
                "show": "我说了不要重复留下您的联系方式，我真的真的会不想理你的-宝~（霸气）",
                "numder": number,
            }
            return render(request, 'index.html', context=context)
    else:
        number = ShouJi.objects.filter(tianjia__exact="No").count()
        context = {
            "show": "请留下您宝贵的联系方式，我会在第一时间联系您。",
            "numder": number,
        }
        return render(request, 'index.html', context=context)


def caxun(request):

    if request.method == 'POST':
        name = request.POST['name']
        try:
            jieguo = CaXun.objects.get(name__exact=name)
        except:
            jieguo = None
        if not name:
            context = {
                "show": "警告！！！请不要提交空白名，不然我会奖励你一朵小红花的哦",
            }
            return render(request, 'caxun.html', context=context)
        elif CaXun.objects.filter(name__exact=name):
            context = {
                "show": "您的课程《" + jieguo.curriculum_name + "》完成进度为: " + jieguo.jingdu + " %",
            }
            return render(request, 'caxun.html', context=context)
        else:
            context = {
                "show": "对不起，您还没有刷课呢",
            }
            return render(request, 'caxun.html', context=context)
    else:
        context = {
            "show": "请输入您的真实姓名。",
        }
        return render(request, 'caxun.html', context=context)
